package com.ciaojian.core.service;

import com.ciaojian.core.enums.business.OrderStatusEnum;
import com.ciaojian.core.model.Charge;
import com.ciaojian.core.model.ChargeLog;
import com.ciaojian.core.model.Order;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 回调Service
 *
 * @author oxiny
 */
public abstract class NotifyService {
    @Resource
    private ChargeUserService chargeUserService;

    public void chargeNotify(Order order) {
        // 修改订单状态
        order.setStatus(OrderStatusEnum.complete);
        order.setPayTime(new Date());
        order.setPayTotal(order.getGoodsTotal());
        order.updateById();

        Charge charge = new Charge().selectById(order.getCouponId());
        // 用户卡金余额
        Integer balance = chargeUserService.getBalance(order.getUserId());

        if (chargeUserService.chargeChange(order.getUserId(), charge.getGetTotal(), 1, 0, order.getId())) {
            // 成功...
            ChargeLog chargeLog = new ChargeLog();
            chargeLog.setUserId(order.getUserId());
            chargeLog.setChargeId(order.getCouponId());
            chargeLog.setTargetType(0);
            chargeLog.setTargetId(order.getId());
            chargeLog.setPrice(order.getPayTotal().intValue());
            chargeLog.setBefBalance(balance);
            chargeLog.setAftBalance(balance + charge.getGetTotal());
            chargeLog.insert();
        } else {
            // 失败...
            // 退款...
            // 记录...
        }
    }

    /**
     * 产品组购买回调
     *
     * @param order 订单
     */
    public abstract void productGroupNotify(Order order);
}
